package com.zhang.management;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

import com.zhang.bean.Course;
import com.zhang.bean.SC;

import zhang.jdbc.graph.Student;
import zhang.utils.JDBCUtils;

import javax.swing.JLabel;
import java.awt.Font;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ChengJiLuRu extends JFrame {

	private JPanel contentPane;
	private JDBCUtils jdb = new JDBCUtils();
	private JTable table;
	private DefaultTableModel model;
	private LinkedList<SC> list = new LinkedList<SC>();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ChengJiLuRu frame = new ChengJiLuRu();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ChengJiLuRu() {
		setDefaultCloseOperation(JFrame.TEXT_CURSOR);
		setBounds(100, 100, 604, 521);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("成绩录入");
		lblNewLabel.setFont(new Font("黑体", Font.PLAIN, 33));
		lblNewLabel.setBounds(219, 10, 155, 64);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("课程名称：");
		lblNewLabel_1.setBounds(29, 87, 75, 15);
		contentPane.add(lblNewLabel_1);
		
		JComboBox dangQianKeCheng = new JComboBox();
		dangQianKeCheng.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				model.getDataVector().removeAllElements();//清空表格
				model.fireTableDataChanged();//清空表格
				List<SC> selectAll2 = jdb.selectAll(SC.class);//查询所有的选课记录
				Iterator<SC> it1 = selectAll2.iterator();
				
				SC t;
				Student tmp;
				
				Object selectedItem = dangQianKeCheng.getSelectedItem();
				Course cou = jdb.selectOne(Course.class, selectedItem.toString());//根据当前所选的下拉框内容查询到课程的记录
				while(it1.hasNext()) {
					t=it1.next();
					if(cou.getid()==t.getCid()) {
						tmp =jdb.selectOne(Student.class, t.getId());
						model.addRow(new String[] {tmp.getname(),t.getUsual_grade()+"",t.getExam_grade()+"",t.getFinal_grade()+"",t.getComment()});
					}
				}
			}
		});
		//初始化课程下拉列表
			Vector<String> v = new Vector<String>();
			List<Course> selectAll = jdb.selectAll(Course.class);
			Iterator<Course> it = selectAll.iterator();
			while(it.hasNext()) {
				v.add(it.next().getName());
			}
		//
		dangQianKeCheng.setModel(new DefaultComboBoxModel(v));
		dangQianKeCheng.setBounds(98, 84, 127, 23);
		contentPane.add(dangQianKeCheng);
		
		JScrollPane scrollPane = new JScrollPane();
		
		
		scrollPane.setBounds(53, 133, 515, 268);
		contentPane.add(scrollPane);
		
		table = new JTable();
		//初始化模式
		model = new DefaultTableModel(new Object[][] {},new String[] {"姓名", "平时成绩", "期末成绩", "最后成绩", "备注"});
		table.setModel(model);
		//查询所以的选课信息
		  
		List<SC> selectAll2 = jdb.selectAll(SC.class);
		Iterator<SC> it1 = selectAll2.iterator();
		
		SC t;
		Student tmp;
		
		Object selectedItem = dangQianKeCheng.getSelectedItem();//获取所选的信息
		
		Course cou = jdb.selectOne(Course.class, selectedItem.toString());//根据课程名查询到课程
		//把集合里面选择对应课程的学生更新到表格
		while(it1.hasNext()) {
			t=it1.next();
			if(cou.getid()==t.getCid()) {
				tmp =jdb.selectOne(Student.class, t.getId());
				model.addRow(new String[] {tmp.getname(),t.getUsual_grade()+"",t.getExam_grade()+"",t.getFinal_grade()+"",t.getComment()});
			}
		}
		scrollPane.setViewportView(table);
		JButton submit = new JButton("提交");
		submit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				list.clear();
				SC t ;
				Student stu;
				Course cou;
				Vector<Vector> dataVector = model.getDataVector();
				for(int i=0;i<dataVector.size();i++) {
					t = new SC();
					stu = jdb.selectOne(Student.class,(String)dataVector.get(i).get(0));
					cou = jdb.selectOne(Course.class, dangQianKeCheng.getSelectedItem().toString());
					t.setUsual_grade(Float.parseFloat((String) dataVector.get(i).get(1)));
					t.setExam_grade(Float.parseFloat((String) dataVector.get(i).get(2)));
					t.setFinal_grade(Float.parseFloat((String) dataVector.get(i).get(3)));
					t.setComment((String) dataVector.get(i).get(4));
					t.setId(stu.getSid());
					t.setCid(cou.getid());
					list.add(t);
				}
				Iterator<SC> it = list.iterator();
				while(it.hasNext()) {
					jdb.update(it.next());
				}
			}
		});
		submit.setBounds(234, 435, 97, 23);
		contentPane.add(submit);
		this.setVisible(true);
	}
}
